"""
@Time ： 2021/12/8 17:54
@Auth ： ChenZhangKun
"""
import threading
import time

g_num = 0


def demo1(num):
    global g_num
    for i in range(num):
        g_num += 1
    print(g_num)


def demo2(num):
    global g_num
    for i in range(num):
        # 在一级缓存中获取g_num的值
        # 执行加一操作 ->这里可能有线程调度问题，导致线程不安全
        # 赋值回去
        g_num += 1
    print(g_num)


if __name__ == '__main__':
    t1 = threading.Thread(target=demo1, args=(1000000,))
    t2 = threading.Thread(target=demo1, args=(1000000,))
    t1.start()
    t2.start()

    time.sleep(6)
    # 有线程问题
    print(g_num)
